package com.wetao.module_user.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.AppCompatTextView;

import com.wetao.lib_common.EventBus.EventBusMessage;
import com.wetao.lib_common.base.BaseLazyFragment;
import com.wetao.lib_common.utils.BaseEvent;
import com.wetao.lib_common.view.OnMultiClickListener;
import com.wetao.module_user.R;
import com.wetao.module_user.R2;
import com.wetao.module_user.contract.UserInfoFragmentCon;
import com.wetao.module_user.presenter.UserInfoFragmentPre;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class UserInfoFragment extends BaseLazyFragment<UserInfoFragmentCon.View, UserInfoFragmentPre> implements
        UserInfoFragmentCon.View {

    AppCompatTextView tvAccount;
    AppCompatTextView tvNameIdentify;
    AppCompatTextView tvPhoneNumber;

    public static UserInfoFragment newInstance(String account, String identify, String phoneNum) {
        Bundle args = new Bundle();
        args.putString("account", account);
        args.putString("identify", identify);
        args.putString("phoneNum", phoneNum);

        UserInfoFragment fragment = new UserInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.user_fragment_info;
    }

    @Override
    public UserInfoFragmentPre createPresenter() {
        return new UserInfoFragmentPre(mContext);
    }

    @Override
    public UserInfoFragmentCon.View createView() {
        return this;
    }

    @Override
    public void init(View rootView) {
        tvAccount = rootView.findViewById(R.id.tv_account);
        tvNameIdentify = rootView.findViewById(R.id.tv_name_identify);
        tvPhoneNumber = rootView.findViewById(R.id.tv_phone_number);

        Bundle bundle = null;
        if ((bundle = getArguments()) != null) {
            String account = bundle.getString("account");
            if (!TextUtils.isEmpty(account)) {
                tvAccount.setText(account);
            }
            String identify = bundle.getString("identify");
            if (!TextUtils.isEmpty(identify)) {
                tvNameIdentify.setText(identify);
            }
            String phoneNum = bundle.getString("phoneNum");
            if (!TextUtils.isEmpty(phoneNum)) {
                tvPhoneNumber.setText(phoneNum);
            }
        }

        tvPhoneNumber.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if(null != onListenClick) {
                    onListenClick.onListenClick();
                }
            }
        });
    }

    @Override
    public void onFragmentFirst() {
        super.onFragmentFirst();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onFragmentInVisible() {
        super.onFragmentInVisible();
    }

    @Override
    public void onFragmentVisble() {
        super.onFragmentVisble();


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void busGetMessage(EventBusMessage messageEvent) {
     if(messageEvent.getAction() == EventBusMessage.EVENT_BUS_ACTION_UPDATE_PHONE) {
         if(!TextUtils.isEmpty(messageEvent.getMsg())) {
             String[] strArray = messageEvent.getMsg().split("###");
             tvPhoneNumber.setText(strArray[0]);
             tvAccount.setText(strArray[1]);
         }
     }
    }

    public interface OnListenClick{
        void onListenClick();
    }
    OnListenClick onListenClick;

    public void setOnListenClick(OnListenClick onListenClick) {
        this.onListenClick = onListenClick;
    }
}
